package com.gxar.quick.ar.server.base.model.protocol;

import com.gxar.common.entity.SystemCodeEnum;
import com.gxar.common.utils.AssertUtils;
import com.gxar.common.utils.StringSpliceUtils;
import com.gxar.common.utils.StringUtils;
import com.gxar.quick.ar.server.base.common.utils.NumberUtils;
import lombok.Data;

import java.util.Objects;

/**
 * AR向量
 *
 * @author zxh
 * @version 1.0
 * @created 2024/01/02.
 */
@Data
public class ArVector3 {

    private Double x;

    private Double y;

    private Double z;

    public ArVector3() {

    }

    public ArVector3(Double x, Double y, Double z) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    @Override
    public String toString() {
        return StringSpliceUtils.splice("{} {} {}",
                NumberUtils.formatDouble(x),
                NumberUtils.formatDouble(y),
                NumberUtils.formatDouble(z));
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ArVector3 arVector3 = (ArVector3) o;
        return Objects.equals(x, arVector3.x) && Objects.equals(y, arVector3.y) && Objects.equals(z, arVector3.z);
    }

    @Override
    public int hashCode() {
        return Objects.hash(x, y, z);
    }

    public static ArVector3 create(Double x, Double y, Double z) {
        return new ArVector3(x, y, z);
    }

    public static ArVector3 create(ArVector3 input) {
        AssertUtils.notNull(input, SystemCodeEnum.PARAM_ERROR.getRet(), "输入不能为空");
        return new ArVector3(input.x, input.y, input.z);
    }

    public static ArVector3 create(ArVector3 input, Double spaceBaseSize) {
        AssertUtils.notNull(input, SystemCodeEnum.PARAM_ERROR.getRet(), "输入不能为空");
        AssertUtils.notNull(spaceBaseSize, SystemCodeEnum.PARAM_ERROR.getRet(), "空间尺度不能为空");
        AssertUtils.isTrue(spaceBaseSize > 0, SystemCodeEnum.PARAM_ERROR.getRet(), "空间尺度需 > 0");
        return new ArVector3(input.x / spaceBaseSize, input.y / spaceBaseSize, input.z / spaceBaseSize);
    }

    public static ArVector3 from(String inputStr) {
        if (StringUtils.isEmpty(inputStr)) {
            return null;
        }

        String[] inputArrays = inputStr.split(" ");
        if (inputArrays.length != 3) {
            return null;
        }

        Double x = Double.parseDouble(inputArrays[0]);
        Double y = Double.parseDouble(inputArrays[1]);
        Double z = Double.parseDouble(inputArrays[2]);

        return new ArVector3(x, y, z);
    }
}
